package cn.agiledata.bank.common.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.struts.taglib.TagUtils;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

import com.vnv.infrastructure.util.PagingResult;

public class PagingTag extends BodyTagSupport {

    private static final long serialVersionUID = 2408733689212016861L;

	private PagingResult pagingResult;

    private String className;

    private int maxPageSize;
    
    private String goImage;

    public void setName(Object name) throws JspException {
        String key = (String) ExpressionEvaluatorManager.evaluate("name", name
                .toString(), Object.class, this, this.pageContext);
        this.pagingResult = (PagingResult) TagUtils.getInstance().lookup(
                this.pageContext, key, null);
    }

    public void setClassName(Object className) throws JspException {
        this.className = (String) ExpressionEvaluatorManager.evaluate(
                "className", className.toString(), Object.class, this,
                this.pageContext);
    }

    public void setMaxPageSize(Object maxPageSize) throws JspException {
        String maxValue = (String) ExpressionEvaluatorManager.evaluate(
                "maxPageSize", maxPageSize.toString(), Object.class, this,
                this.pageContext);
        this.maxPageSize = Integer.parseInt(maxValue);
    }
    
    public void setGoImage(Object goImage) throws JspException {
        this.goImage = (String) ExpressionEvaluatorManager.evaluate("goImage",
                goImage.toString(), Object.class, this, this.pageContext);
    }

    public int doEndTag() throws JspException {
        String s = "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n"
                + "<tr><td height=\"25\" align=\"center\" nowrap>\n"
                + "共 <span class=\"orangefont14b\">"
                + this.pagingResult.getTotalCount()
                + "</span> 条 每页显示 <span class=\"orangefont14b\">" + this.pagingResult.getPageSize() + "</span> 条 \n"
                + "第<span class=\"orangefont14b\">" + this.pagingResult.getPageNo() 
                + "</span>页/共<span class=\"orangefont14b\">" + this.pagingResult.getPageCount() + "</span>页\n"
                + "<a class=\"menuTextBlue2\" href=\"#\" onClick=\"dosubmit('1');\">首页</a>\n"
                + "<a class=\"menuTextBlue2\" href=\"#\" onClick=\"dosubmit('" + this.pagingResult.getPrevious() + "');\">上一页</a>\n"
                + "<a class=\"menuTextBlue2\" href=\"#\" onClick=\"dosubmit('" + this.pagingResult.getNext() + "');\">下一页</a>\n"
                + "<a class=\"menuTextBlue2\" href=\"#\" onClick=\"dosubmit('" + this.pagingResult.getPageCount() + "');\">尾页</a>\n"
                + "转到：" + "<input type=\"text\" id=\"paging_toPageNo\" size=\"1\" onkeyup=\"id_keyup(this)\" class=\"inputPageStyle\"> 页\n";
        if (this.goImage != null && this.goImage.length() > 0) {
            s += "<a href=\"#\" onclick=\"dosubmitGo(this.form)\"><img src=\"" + this.goImage + "\" border=\"0\" /></a>\n";
        } else {
            s += "<input type=\"button\" value=\"GO\" onclick=\"dosubmitGo(this.form)\" class=\"buttonStyle-2word\" align=\"absmiddle\">\n";
        }
        s += "<input type=\"hidden\" name=\"pageNo\" value=\"\">\n"
                + "<input type=\"hidden\" name=\"pageSize\" value=\"" + this.pagingResult.getPageSize() + "\">\n"
                + "</td></tr></table>\n"
                + "<script language=\"JavaScript\" type=\"text/javascript\">\n"
                + "var currentPageNo = " + this.pagingResult.getPageNo() + ";\n"
                + "var currentPageSize = " + this.pagingResult.getPageSize() + ";\n"
                + "function id_keyup(txtinput){\n"
                + "  txtinput.value=txtinput.value.replace(/(^\\s*)|(\\s*$)/g, \"\");\n"
                + "}\n"
                + "function getFormObject(){\n"
                + "  return document.getElementById('paging_toPageNo').form;\n"
                + "}\n"
                + "function dosubmit(pageNo){\n"
                + "  var formObj = getFormObject();\n"
                + "  formObj.pageNo.value=pageNo;\n"
                + "  formObj.submit();\n"
                + "}\n"
                + "function dosubmitGo(){\n"
                + "  var formObj = getFormObject();\n"
                + "  var pageSizeObj = formObj.pageSize;\n"
                + "  if(pageSizeObj.value.indexOf('.')>0 || isNaN(pageSizeObj.value) || parseInt(pageSizeObj.value) < 1){\n"
                + "    alert('每页条数必须为大于0的整数！');\n"
                + "    pageSizeObj.value = currentPageSize;\n"
                + "    pageSizeObj.focus();\n" + "    return;\n" + "  }\n"
                + "  if(pageSizeObj.value == ''){\n"
                + "    alert('每页条数不能为空！');\n"
                + "    pageSizeObj.value = currentPageSize;\n"
                + "    pageSizeObj.focus();\n" 
                + "    return;\n" 
                + "  }\n";
        if (this.maxPageSize > 0) {
            s += "  var maxPageSize = " + this.maxPageSize + ";\n"
                    + "  if(pageSizeObj.value > maxPageSize ){\n"
                    + "    alert('每页条数不能大于'+maxPageSize+'！');\n"
                    + "    pageSizeObj.value = currentPageSize;\n"
                    + "    pageSizeObj.focus();\n" 
                    + "    return;\n" 
                    + "  }\n";
        }
        s += "  var toPageNoObj = document.getElementById('paging_toPageNo');\n"
                + "  if(toPageNoObj.value.indexOf('.')>0 || isNaN(toPageNoObj.value) || parseInt(toPageNoObj.value) < 1){\n"
                + "    alert('页码必须为大于0的整数！');\n"
                + "    toPageNoObj.value = currentPageNo;\n"
                + "    toPageNoObj.focus();\n"
                + "    return;\n"
                + "  }\n"
                + "  if(toPageNoObj.value == ''){\n"
                + "    alert('页码不能为空！');\n"
                + "    toPageNoObj.value = currentPageNo;\n"
                + "    toPageNoObj.focus();\n"
                + "    return;\n"
                + "  }\n"
                + "  var pageCount = " + this.pagingResult.getPageCount() + ";\n"
                + "  if(toPageNoObj.value > pageCount ){\n"
                + "    alert('页码不能大于总页数！');\n"
                + "    toPageNoObj.value = currentPageNo;\n"
                + "    toPageNoObj.focus();\n"
                + "    return;\n"
                + "  }\n"
                + "  formObj.pageNo.value = toPageNoObj.value;\n"
                + "  formObj.submit();\n"
                + "}\n"
                + "</script>\n";

        TagUtils.getInstance().write(this.pageContext, s);
        return super.doEndTag();
    }
}
